Basic Python Programming

Python Imaging Library(PIL)

The most important python imaging library is Image. It is imported from PIL like this

Opening image

To open an image, use the .open() function from the Image module. Specify the path in the .open() function. Here the file path is just the filename, 'colorshape.png' which is in the same directory as the .ipnb file itself. It is to read from the disk.

To print the format of the image use the .format attribute. If it is not read from the file, it will return a None. Here the format is 'JPEG' format.

The image size is given by .size() function from the Image module. It gives the width and height in pixels and is a tuple type. The width here is 186 pixels and the height is 271 pixels.

The mode of the image is given by the .mode attribute which gives the number and names of the band in the image. Here, the mode is 'RGB' which means the image is in Red, Blue, Green or in the true colors.

To show the image, use the .show() method which takes in no parameters. It is not very efficient as it stores the image in a temporary utility and displays it from that utility.

Enhance images

Enhancing images can be possible by ImageEnhance package from the PIL library in python. The brightness, contrast, sharpness, etc. are a few enhancing factors.

The class, Brightness is used to change the brightness of the image. The enhance() method is used to specify the factor with which the the image should be modified.

The class, Sharpness is used to change the brightness of the image. The enhance() method is used to specify the factor with which the the image should be modified.

Pasting images

Images can be pasted on top of each other using the paste() option. It takes in three parameters which are image on which the other image is to be pasted, the image to be pasted and a tuple of coordinates of the top left corner of where the pasted image should start from where the image is to be pasted. This modifies the base image. So here the image2's top left corner is (20,30) from (0,0) or image1's top left corner.

Here, the second image is been cropped out because it has a bigger width than the width of the base image. So use the resize option to get the image to fit in without cropping using resize() method which takes in two parameters that are image half width and image half height.

To create an empty canvas, use new() method which takes in the mode, size and color of the canvas. The mode is 'RGB' here and the size is 200 width pixels by 300 height pixels. Pasting on top of canvas is very useful as well.

Convert images into different scales is done by the .convert() function which takes in mode which is compulsory.